angular.module('videocontrollers', [])
  //视频详情的Controller
  .controller('VideoDetailCtrl',function($scope,$state,$stateParams,Loading,Toast,$sce,ENV,Video,VideoComment,Storage,History){
      var videoId = $stateParams['videoId'];
      var memberId = Storage.get("member").member_id;
      $scope.picPath = ENV.picPath;
      $scope.videoPath = ENV.videoPath;
      $scope.isAndroid = ionic.Platform.isAndroid();//是否是安卓系统
      $scope.isIOS = ionic.Platform.isIOS();//是否是IOS系统
      $scope.isInnerVideo  = false;//是否是内部视频
     var videoPlayHtml = '';
      //返回
      $scope.goback = function(){
        History.goback();
      }
    $scope.trustSrc = function(src) {
      return $sce.trustAsResourceUrl(src);
    }
    //刚刚进入页面的时候调用
    $scope.$on('$ionicView.enter',function(){
      if($scope.videoDetail&&!$scope.isInnerVideo){
        $scope.isActive = true;
        $scope.videoDetail.video_addr = $sce.trustAsHtml(videoPlayHtml);
      }
    });
    //页面离开的时候调用
    $scope.$on('$ionicView.beforeLeave',function(){
     // if(ionic.Platform.isAndroid()){
        $scope.isActive = false;
        $scope.videoDetail.video_addr = '';
     // }
    });
    //获取视频详情
    Loading.showLoading();
    Video.getVideoDetail(memberId,videoId);
    //获取网络数据成功的后调用该方法
    $scope.$on('Video.videoDetailUpdated', function(){
      Loading.hideLoading();
      $scope.videoDetail = Video.getVideoDetailJson();
      $scope.videoPath = ENV.videoPath+$scope.videoDetail.video_addr;
      if($scope.videoDetail.video_addr.indexOf("iframe")>=0){
        $scope.isInnerVideo = false;
        videoPlayHtml = $scope.videoDetail.video_addr;
        $scope.videoDetail.video_addr = $sce.trustAsHtml($scope.videoDetail.video_addr);
        $scope.isActive = true;
      }else{
        $scope.isInnerVideo = true;
      }
    })
    //进入页面时
    $scope.$on('$ionicView.beforeEnter',function(){
      memberId = Storage.get("member").member_id;
      if($scope.videoDetail!=undefined){
        VideoComment.getCommentCount(videoId,memberId);
      }
    });
    //当service访问网络成功时，调用该方法
    $scope.$on('Video.CommentCountUpdated', function(){
      var result  = VideoComment.getCommetCountJson();
      $scope.videoDetail.video_comment_num = result.video_comment_num;
      $scope.videoDetail.lvStatus = result.lvStatus;
      $scope.videoDetail.vcStatus = result.vcStatus;
    });

      //收藏该视频
      var collectVideoing = false;
      $scope.collectVideo = function(){
          if(!collectVideoing) {
            Loading.showLoading();
            Video.collectVideo(memberId,videoId);
            collectVideoing = true;
          }
      };
    //获取网络数据成功的后调用该方法
      $scope.$on('Video.collectVideoUpdated',function(){
        Loading.hideLoading();
        collectVideoing = false;
        var result = Video.getCollectJson();
        $scope.videoDetail.vcStatus = result.vcStatus;
        Toast.showMessage(result.vcMsg);
      });
      //点赞该视频
      var isLikeVideoing = false;
      $scope.likeVideo = function(){
        if(!isLikeVideoing){
          Loading.showLoading();
          Video.likeVideo(memberId,videoId);
          isLikeVideoing=true;
        }
      };
     //获取网络数据成功的后调用该方法
    $scope.$on('Video.likeVideoUpdated',function(){
      Loading.hideLoading();
      isLikeVideoing = false;
      var result = Video.getLikeJson();
      $scope.videoDetail.lvStatus = result.lvStatus;
      Toast.showMessage(result.lvMsg);
    });
  })

  .controller("VideoCommentCtrl",function($scope,$stateParams,$ionicPopup,Loading,Toast,VideoComment,ENV,History,Storage){

    var videoId=$stateParams['videoId'];
    var memberId = '';
    var isFirstIn = true;
    $scope.picPath = ENV.picPath;
    $scope.$on('$ionicView.beforeEnter',function(){
      memberId = Storage.get('member').member_id;
      if(isFirstIn){
        Loading.showLoading();
        isFirstIn = false;
      }
      //获取评论列表
      VideoComment.getCommentList(videoId,memberId);
    });
    //删除评论
    var deleteIndex = '';
    $scope.deleteComment = function(commentId,index){
      $ionicPopup.confirm({
        template: '<p style="text-align: center;font-size:16px;">确定要删除该评论吗？</p>',
        cancelText: '取消',
        okText: '确定',
        okType: 'hd-red-btn',
        cancelType: 'cancel-esc-btn'
      }).then(function (res) {
        if (res) {
          Loading.showLoading();
          VideoComment.deleteComment(commentId,memberId,videoId);
          deleteIndex = index;
        }
      });

    }
    $scope.$on('VideoComment.deleteCommentUpdated',function(){
      Loading.hideLoading();
      var result = VideoComment.getDelCommentJson();
      if(result.dvCommentStatus){
        $scope.videoCommentList.splice(deleteIndex,1);
      }
      Toast.showMessage(result.dvCommentMsg);
    });
    //返回
    $scope.goback = function(){
      History.goback();
    }

    $scope.videoCommentList = [];
    //新增评论
    $scope.comment={
      newComment:''
    };
    $scope.doRefresh = function(){
      VideoComment.getCommentList(videoId,memberId);
    }
    $scope.loadMore = function(){
      VideoComment.getMoreCommentList(videoId,memberId);
    }
    $scope.hasNextPage = function(){
      return VideoComment.hasNextPage();
    }
    //请求网络数据完成之后
    $scope.$on('Video.CommentUpdated', function() {
      Loading.hideLoading();
      $scope.$broadcast('scroll.refreshComplete');
      $scope.videoCommentList = VideoComment.getCommentJson();
      $scope.memberInfo = VideoComment.getMemberInfo();
      $scope.videoTitle = VideoComment.getVideoTitleJson();
    });
    $scope.$on('Video.MoreCommentListUpdated',function(){
      $scope.$broadcast('scroll.infiniteScrollComplete');
      $scope.videoCommentList = VideoComment.getCommentJson();
    });
    //发表评论
    $scope.publishComment = function(){
      if($scope.comment.newComment == ''){
        Toast.showMessage("评论内容不能为空");
      }else{
        Loading.showLoading();
        VideoComment.publishComment(memberId,videoId,$scope.comment.newComment);
        $scope.comment.newComment = '';//使得输入框中的值为空
      }
    };
    //请求网络数据完成之后
    $scope.$on('Video.publishCommentUpdated', function() {
      Loading.hideLoading();
      var newComment = VideoComment.getNewComment();
      if(newComment.vcommentStatus){
        $scope.videoCommentList.unshift(newComment);
      }
      Toast.showMessage(newComment.vcommentMsg);
    });
  })
;
